package com.study.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.exception.BusinessException;
import com.study.exception.ErrorCode;
import com.study.exception.ThrowUtils;
import com.study.mapper.LearningNoteMapper;
import com.study.model.dto.note.LearningNoteAddRequest;
import com.study.model.dto.note.LearningNoteQueryRequest;
import com.study.model.dto.note.LearningNoteUpdateRequest;
import com.study.model.entity.LearningNote;
import com.study.model.entity.User;
import com.study.model.vo.LearningNoteVO;
import com.study.service.LearningNoteService;

import com.study.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @description 针对表【learning_note(学习笔记)】的数据库操作Service实现
* @createDate 2025-02-18 10:53:59
*/
@Service
public class LearningNoteServiceImpl extends ServiceImpl<LearningNoteMapper, LearningNote>
    implements LearningNoteService {
    @Autowired
    private UserService userService;
    /**
     * 创建学习笔记
     *
     * @param noteAddRequest
     * @return
     */
    @Override
    public Long createLearningNote(LearningNoteAddRequest noteAddRequest, HttpServletRequest request) {
        if (noteAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (noteAddRequest.getTitle() == null) {
            noteAddRequest.setTitle("无标题");
        }
        LearningNote note = new LearningNote();
        BeanUtils.copyProperties(noteAddRequest, note);
        note.setUserId(userService.getLoginUser(request).getId());
        boolean save = this.save(note);
        if (!save) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "创建失败");
        }
        return note.getId();
    }

    /**
     * 根据id查询学习笔记包装类
     *
     * @param id
     * @return
     */
    @Override
    public LearningNoteVO getLearningNoteVOById(long id) {
        LearningNote learningNote = this.baseMapper.selectById(id);
        ThrowUtils.throwIf(learningNote == null, ErrorCode.NOT_FOUND_ERROR);
        LearningNoteVO learningNoteVO = new LearningNoteVO();
        BeanUtils.copyProperties(learningNote, learningNoteVO);
        return learningNoteVO;

    }

    /**
     * 更新学习笔记
     *
     * @param noteUpdateRequest
     * @return
     */
    @Override
    public Boolean updateLearningNote(LearningNoteUpdateRequest noteUpdateRequest) {
        if (noteUpdateRequest == null || noteUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        LearningNote learningNote = this.baseMapper.selectById(noteUpdateRequest.getId());
        BeanUtils.copyProperties(noteUpdateRequest, learningNote);
        return this.updateById(learningNote);
    }

    /**
     * 分页获取学习笔记封装类
     *
     * @param noteQueryRequest
     * @return
     */
    @Override
    public Page<LearningNoteVO> listLearningNoteVOByPage(LearningNoteQueryRequest noteQueryRequest) {
        if (noteQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = noteQueryRequest.getCurrent();
        long size = noteQueryRequest.getPageSize();
        Page<LearningNote> notePage = page(new Page<>(current, size),getQueryWrapper(noteQueryRequest));
        Page<LearningNoteVO> noteVOPage= new Page<>(current, size, notePage.getTotal());
        List<LearningNoteVO> noteVOList = getNoteVOList(notePage.getRecords());
        noteVOPage.setRecords(noteVOList);
        return noteVOPage;
    }

    /**
     * 获取学习笔记VO列表
     * @param noteList
     * @return
     */
    private List<LearningNoteVO> getNoteVOList(List<LearningNote> noteList) {
        if (CollUtil.isEmpty(noteList)) {
            return new ArrayList<>();
        }
        return noteList.stream().map(this::getNoteVO).collect(Collectors.toList());
    }

    private LearningNoteVO getNoteVO(LearningNote learningNote) {
        if(learningNote == null){
            return null;
        }
        LearningNoteVO noteVO = new LearningNoteVO();
        BeanUtils.copyProperties(learningNote, noteVO);
        return noteVO;
    }

    /**
     * 获取学习笔记分页QueryWrapper
     * @param noteQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<LearningNote> getQueryWrapper(LearningNoteQueryRequest noteQueryRequest) {
        if (noteQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = noteQueryRequest.getId();
        Long userId = noteQueryRequest.getUserId();
        String title = noteQueryRequest.getTitle();
        String content = noteQueryRequest.getContent();
        String sortField = noteQueryRequest.getSortField();
        String sortOrder = noteQueryRequest.getSortOrder();

        QueryWrapper<LearningNote> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjUtil.isNotNull(id), "id", id);
        queryWrapper.eq(ObjUtil.isNotNull(userId), "userId", userId);
        queryWrapper.like(StrUtil.isNotBlank(title), "title", title);
        queryWrapper.like(StrUtil.isNotBlank(content), "content", content);
        queryWrapper.orderBy(StrUtil.isNotEmpty(sortField), sortOrder.equals("ascend"), sortField);
        return queryWrapper;
    }
}




